package vip.liux.contracts.shared.properties;

import lombok.Data;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.util.*;

@Data
public class FileIconProperties {

    @NestedConfigurationProperty
    private FileIconInfo defaultIconInfo;

    @NestedConfigurationProperty
    private Map<String, FileIconInfo> fileIconInfos = new HashMap<>();

    public FileIconProperties() {
        setDefaultIcon(null);
        setArchiveIcons(null);
        setAudioIcons(null);
        setExcelIcons(null);
        setImageIcons(null);
        setVideoIcons(null);
        setPdfIcons(null);
        setPowerPointIcons(null);
        setWordIcons(null);
        setCodeIcons(null);
    }

    public FileIconInfo getFileIconInfo(String fileName) {
        String[] fileNameParts = fileName.split("\\.");

        if (fileNameParts.length == 1) {
            return defaultIconInfo;
        }

        String extension = fileNameParts[fileNameParts.length - 1].toLowerCase();
        return fileIconInfos.getOrDefault(extension, defaultIconInfo);
    }

    public FileIconProperties setFileIcon(String extension, FileIconInfo iconInfo) {
        extension = extension.toLowerCase();
        fileIconInfos.put(extension, iconInfo);
        return this;
    }

    public FileIconProperties setFileIcons(List<String> extensions, FileIconInfo iconInfo) {
        for (String extension : extensions) {
            setFileIcon(extension, iconInfo);
        }
        return this;
    }

    public FileIconProperties setDefaultIcon(FileIconInfo iconInfo) {
        if (iconInfo == null) {
            iconInfo = new FileIconInfo("far fa-file", FileIconType.FontAwesome);
        }
        defaultIconInfo = iconInfo;
        return this;
    }

    public FileIconProperties setArchiveIcons(FileIconInfo iconInfo) {
        List<String> extensions = Arrays.asList("zip", "rar", "gz", "bz2", "7z");
        if (iconInfo == null) {
            iconInfo = new FileIconInfo("far fa-file-archive", FileIconType.FontAwesome);
        }
        setFileIcons(extensions, iconInfo);
        return this;
    }

    public FileIconProperties setAudioIcons(FileIconInfo iconInfo) {
        List<String> extensions = Arrays.asList("pcm", "wav", "aiff", "mp3", "aac", "ogg", "wma", "flac", "alac");
        if (iconInfo == null) {
            iconInfo = new FileIconInfo("far fa-file-audio", FileIconType.FontAwesome);
        }
        setFileIcons(extensions, iconInfo);
        return this;
    }

    public FileIconProperties setExcelIcons(FileIconInfo iconInfo) {
        List<String> extensions = Arrays.asList("xlsx", "xlsm", "xlsb", "xltx", "xltm", "xls", "xlt", "xlw", "xlr");
        if (iconInfo == null) {
            iconInfo = new FileIconInfo("far fa-file-excel", FileIconType.FontAwesome);
        }
        setFileIcons(extensions, iconInfo);
        return this;
    }

    public FileIconProperties setImageIcons(FileIconInfo iconInfo) {
        List<String> extensions = Arrays.asList("tif", "tiff", "bmp", "jpg", "jpeg", "gif", "png", "eps", "webp");
        if (iconInfo == null) {
            iconInfo = new FileIconInfo("far fa-file-image", FileIconType.FontAwesome);
        }
        setFileIcons(extensions, iconInfo);
        return this;
    }

    public FileIconProperties setVideoIcons(FileIconInfo iconInfo) {
        List<String> extensions = Arrays.asList("avchd", "avi", "flv", "mkv", "mov", "mp4", "webm", "wmv");
        if (iconInfo == null) {
            iconInfo = new FileIconInfo("far fa-file-video", FileIconType.FontAwesome);
        }
        setFileIcons(extensions, iconInfo);
        return this;
    }

    public FileIconProperties setPdfIcons(FileIconInfo iconInfo) {
        List<String> extensions = Collections.singletonList("pdf");
        if (iconInfo == null) {
            iconInfo = new FileIconInfo("far fa-file-pdf", FileIconType.FontAwesome);
        }
        setFileIcons(extensions, iconInfo);
        return this;
    }

    public FileIconProperties setPowerPointIcons(FileIconInfo iconInfo) {
        List<String> extensions = Arrays.asList("pptx", "pptm", "ppt", "xps", "potx", "potm", "pot", "ppsx", "ppsm", "pps");
        if (iconInfo == null) {
            iconInfo = new FileIconInfo("far fa-file-powerpoint", FileIconType.FontAwesome);
        }
        setFileIcons(extensions, iconInfo);
        return this;
    }

    public FileIconProperties setWordIcons(FileIconInfo iconInfo) {
        List<String> extensions = Arrays.asList("doc", "docm", "docx", "dot", "dotm", "dotx");
        if (iconInfo == null) {
            iconInfo = new FileIconInfo("far fa-file-word", FileIconType.FontAwesome);
        }
        setFileIcons(extensions, iconInfo);
        return this;
    }

    public FileIconProperties setCodeIcons(FileIconInfo iconInfo) {
        List<String> extensions = Arrays.asList("css", "js", "ts", "cshtml", "cs", "php", "html", "json", "xml", "rb", "py", "java", "c", "cpp", "h", "vb", "csproj");
        if (iconInfo == null) {
            iconInfo = new FileIconInfo("far fa-file-password", FileIconType.FontAwesome);
        }
        setFileIcons(extensions, iconInfo);
        return this;
    }
}
